/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connexion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ISMAYLOULABBIHI
 */
// la classe connection avec la base de donnee MySQL
public class ConnectionManager {

    private Connection conn;

    public ConnectionManager() {

        try {
            //driver de connection
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            javax.swing.JOptionPane.showMessageDialog(null, "Erreur Driver  ", "Etat de L'opération", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            //nom server, utlisateur et le nom de DB 
            conn = DriverManager.getConnection("jdbc:mysql://localhost/gestion_des_emplois?useUnicode=true&characterEncoding=cp1256", "root", "");

        } catch (SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(null, "Erreur Connection Base de donnée", "Etat de L'opération", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Methode pour l'affichage des donnée de DB
    public ResultSet getDataBase(String req) {
        Vector res = new Vector();
        Statement s = null;
        ResultSet rs = null;
        try {
            s = conn.createStatement();
        } catch (SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(null, "Erreur get data", "Etat de L'op�ration", javax.swing.JOptionPane.INFORMATION_MESSAGE);
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            rs = s.executeQuery(req);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    //Methode pour modifier les donnée de DB
    public int setDataBase(String req) {
        int i = 0;
        try {
            Statement s = conn.createStatement();
            i = s.executeUpdate(req);
        } catch (SQLException ex) {
            System.out.println("erreur" + req  +"\n"+ ex.getMessage());
        }
        return i;
    }
    //fonction mai pour teste la class connetin
    public static void main(String[] arg) throws SQLException {
        ConnectionManager c = new ConnectionManager();
        ResultSet rs = c.getDataBase("select * from admin");
        if (rs.next()) {
            System.out.println(rs.getString(1) + " " + rs.getString(2));
        }

    }
}
